package utils
{
	import flash.events.EventDispatcher;
	import flash.filesystem.File;
	
	[Event(type="utils.DirectoryBrowserEvent",name="fileSelected")]
	[Event(type="utils.DirectoryBrowserEvent",name="directoryChanged")]
	[Event(tyep="utils.DirectoryBrowserEvent",name="directoryUp")]
	
	public class DirectoryBrowser extends EventDispatcher
	{
		private var _currentDir:File;
		public function get currentDir():File
		{
			return _currentDir;
		}
		
		public function DirectoryBrowser(initalDir:File)
		{
			if(initalDir.isDirectory)
				_currentDir=initalDir;
		}
		
		public function getListing():Array{
			return _currentDir.getDirectoryListing();
		}
		
		public function up():void{
			if(_currentDir.parent){
				var file:File=_currentDir.resolvePath(_currentDir.parent.nativePath);
				_currentDir=file;
				dispatchEvent(new DirectoryBrowserEvent(file,DirectoryBrowserEvent.DIRECTORY_UP));
			}
			
		}
		
		public function select(name:String):void{
			var file:File=_currentDir.resolvePath(name);
			if(file.isDirectory){
				_currentDir=file;
				dispatchEvent(new DirectoryBrowserEvent(file,DirectoryBrowserEvent.DIRECTORY_CHANGED));
			}
			else{
				dispatchEvent(new DirectoryBrowserEvent(file,DirectoryBrowserEvent.FILE_SELECTED));
				file.openWithDefaultApplication();
			}
		}
	}
}